﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyOA.Model;
using System.Data;
using System.Data.SqlClient;

namespace MyOA.DAL
{
    public class MessageService
    {
        DBHelper db = new DBHelper();
        UserInfoService userInfoService = new UserInfoService();
        /// <summary>
        /// 根据UserId获取所有发信箱信息
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="size">查找条数</param>
        /// <param name="page">查找页数</param>
        /// <param name="isCount">是否为首次加载1、0</param>
        /// <param name="sum">首次查找返回总条数</param>
        /// <returns>返回查找集合</returns>
        public IList<Message> GetAllSendMessageByUserId(int userId, int size, int page, int isCount, out int sum)
        {

            IList<Message> messageList = GetMessageByParameter("proc_getAllSendMessageByUserId", userId, size, page, isCount, out sum);
            db.CloseConnection();
            return messageList;
        }
        /// <summary>
        /// 根据UserId获取所有草稿纸信息
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="size">查找条数</param>
        /// <param name="page">查找页数</param>
        /// <param name="isCount">是否为首次加载1、0</param>
        /// <param name="sum">首次查找返回总条数</param>
        /// <returns>返回查找集合</returns>
        
        public IList<Message> GetAllDraftMessageByUserId(int userId, int size, int page, int isCount, out int sum)
        {
            IList<Message> messageList = GetMessageByParameter("proc_getAllDraftMessageByUserId", userId, size, page, isCount, out sum);
            db.CloseConnection();
            return messageList;
        }
        /// <summary>
        /// 根据UserId获取所有收信箱信息
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="size">查找条数</param>
        /// <param name="page">查找页数</param>
        /// <param name="isCount">是否为首次加载1、0</param>
        /// <param name="sum">首次查找返回总条数</param>
        /// <returns>返回查找集合</returns>
        
        public IList<Message> GetAllReceiveMessageByUserId(int userId, int size, int page, int isCount, out int sum)
        {

            IList<Message> messageList = GetMessageByParameter("proc_getAllReceiveMessageByUserId", userId, size, page, isCount, out sum);
            db.CloseConnection();
            return messageList;
        }
        /// <summary>
        ///根据UserId获取所有已删除信息
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="size">查找条数</param>
        /// <param name="page">查找页数</param>
        /// <param name="isCount">是否为首次加载1、0</param>
        /// <param name="sum">首次查找返回总条数</param>
        /// <returns>返回查找集合</returns>
       
        public IList<Message> GetAllRecycleMessageByUserId(int userId, int size, int page, int isCount, out int sum)
        {
            IList<Message> messageList = GetMessageByParameter("proc_getAllRecycleMessageByUserId", userId, size, page, isCount, out sum);
            db.CloseConnection();
            return messageList;
        }
        /// <summary>
        /// 根据UserId获取用户所有已读信箱信息
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="size">查找条数</param>
        /// <param name="page">查找页数</param>
        /// <param name="isCount">是否为首次加载1、0</param>
        /// <param name="sum">首次查找返回总条数</param>
        /// <returns>返回查找集合</returns>
        public IList<Message> getAllReadReceiveMessageByUserId(int userId, int size, int page, int isCount, out int sum)
        {
            IList<Message> messageList = GetMessageByParameter("proc_getAllReadReceiveMessageByUserId",userId,size,page,isCount,out sum );
            db.CloseConnection();
            return messageList;
        }
        /// <summary>
        /// 根据UserId获取用户所有未读信箱信息
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="size">查找条数</param>
        /// <param name="page">查找页数</param>
        /// <param name="isCount">是否为首次加载1、0</param>
        /// <param name="sum">首次查找返回总条数</param>
        /// <returns>返回查找集合</returns>
        public IList<Message> getAllNoReadReceiveMessageByUserId(int userId,int size,int page,int isCount,out int sum)
        {
            IList<Message> messageList = GetMessageByParameter("proc_getAllNoReadReceiveMessageByUserId", userId, size, page, isCount, out sum);
            db.CloseConnection();
            return messageList;
        }
        public IList<Message> GetMessageBySql(string sql, int userId)
        {
            try
            {
                List<Message> messageList = new List<Message>();
                DataTable table = db.getDataTable(sql, new SqlParameter("@userId", userId));
                foreach (DataRow row in table.Rows)
                {
                    Message message = new Message();
                    message.MessageId = (int)row["MessageId"];
                    message.MsgTitle = (string)row["MsgTitle"];
                    message.MsgContent = (string)row["MsgContent"];
                    message.IfPublish = (int)row["IfPublish"];
                    int fromUserId = (int)row["FromUserId"];
                    message.FromUser = userInfoService.GetUserInfoByUserId(fromUserId);
                    message.RecordTime = (DateTime)row["RecordTime"];
                    messageList.Add(message);
                }
                db.CloseConnection();
                return messageList;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
        private IList<Message> GetMessageByParameter(string StoredProcedure, int userId, int size, int page, int isCount, out int sum)
        {
            try
            {
                sum = 0;
                List<Message> messageList = new List<Message>();
                SqlParameter[] para = new SqlParameter[]{
                new SqlParameter("@page",page),
                new SqlParameter("@size",size),
                new SqlParameter("@IsCount",isCount),
                new SqlParameter("@userId",userId),
                new SqlParameter("@sum",sum )
            };
                SqlCommand command = new SqlCommand(StoredProcedure, db.Connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddRange(para);
                command.Parameters["@sum"].Direction = ParameterDirection.Output;
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Message message = new Message();
                    message.MessageId = (int)reader["MessageId"];
                    message.MsgTitle = (string)reader["MsgTitle"];
                    message.MsgContent = (string)reader["MsgContent"];
                    message.IfPublish = (int)reader["IfPublish"];
                    int fromUserId = (int)reader["FromUserId"];
                    message.RecordTime = (DateTime)reader["RecordTime"];
                    message.FromUser = userInfoService.GetUserInfoByUserId(fromUserId);
                    messageList.Add(message);
                }
                reader.Close();
                sum = (int)command.Parameters["@sum"].Value;
                db.CloseConnection();
                return messageList;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }

        /// <summary>
        /// 根据messageId修改Message
        /// </summary>
        /// <param name="message">要修改的信息</param>
        /// <returns></returns>
        public int updateMessageByMessageId(Message message)
        {
            SqlParameter[] para = new SqlParameter[]{
                new SqlParameter("@messageId",message.MessageId),
                new SqlParameter("@msgTitle",message.MsgTitle),
                new SqlParameter("@msgContent",message.MsgContent),
                new SqlParameter("@ifPublish",message.IfPublish),
                new SqlParameter("@fromUserId",message.FromUser.UserId),
                new SqlParameter("@recordTime",message.RecordTime)
            };
            int result =db.getCount("proc_updateMessageByMessageId", para);
            db.CloseConnection();
            return result;
        }
        /// <summary>
        /// 向表Message中插入新信息并查找该信息MessageId
        /// </summary>
        /// <param name="message"></param>
        /// <returns>新插入的messageId</returns>
        public int insertMessage(Message message)
        {
            SqlParameter[] para = new SqlParameter[]{
                new SqlParameter("@msgTitle",message.MsgTitle),
                new SqlParameter("@msgContent",message.MsgContent),
                new SqlParameter("@recordTime",message.RecordTime),
                new SqlParameter("@ifPublish",message.IfPublish),
                new SqlParameter("@fromUserId",message.FromUser.UserId)
            };
            int result = Convert.ToInt32(db.getScalar("proc_insertMessage", para));
            db.CloseConnection();
            return result;
        }
        /// <summary>
        /// 根据messageId删除Message
        /// </summary>
        /// <param name="messageId">要删除的信息Id</param>
        /// <returns></returns>

        public int deleteMessageByMessageId(int messageId)
        {
            int result =db.getCount("proc_deleteMessageByMessageId", new SqlParameter("@messageId", messageId));
            db.CloseConnection();
            return result;
        }
        /// <summary>
        /// 根据messageId得到Message
        /// </summary>
        /// <param name="messageId">要得到的信息Id<Id/param>
        /// <returns></returns>
        public Message getMessageByMessageId(int messageId)
        {
            try
            {
                Message message = new Message();
                SqlDataReader reader = db.getDataReader("proc_getMessageByMessageId",new SqlParameter("@messageId",messageId));
                if (reader.Read())
                {
                    message.MessageId = (int)reader["MessageId"];
                    message.MsgTitle = (string)reader["MsgTitle"];
                    message.MsgContent = (string)reader["MsgContent"];
                    message.IfPublish = (int)reader["IfPublish"];
                    int fromUserId = (int)reader["FromUserId"];
                    message.RecordTime = (DateTime)reader["RecordTime"];
                    message.FromUser = userInfoService.GetUserInfoByUserId(fromUserId);
                }
                reader.Close();
                db.CloseConnection();
                return message;

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
    }
}
